home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / PlayerPRO 4.4.1 / Filters Plugs / Echo.c < prev    next >
C/C++ Source or Header  |  1995-04-16  |  5KB  |  212 lines

  1. /*    Echo            */
  2. /*    v 0.3            */
  3. /*    1995 by Liane    */
  4.  
  5. //    Usage:
  6. //    Simulates an echo.
  7. //    You can set the timing (ms), which is computed
  8. //    for a F#5.
  9. //    Works on the selected part or all the waveform
  10. //    if there is no selection.
  11.  
  12. #include <Dialogs.h>
  13. #include "MAD.h"
  14. #include "PPPlug.h"
  15.  
  16. #if defined(powerc) || defined(__powerc)
  17. enum {
  18.         PlayerPROPlug = kCStackBased
  19.         | RESULT_SIZE(SIZE_CODE( sizeof(OSErr)))
  20.         | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof( Ptr*)))
  21.         | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof( struct FileInstrData*)))
  22.         | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof( long)))
  23.         | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof( long)))
  24.         | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof( PPInfoPlug*)))
  25. };
  26.  
  27. ProcInfoType __procinfo = PlayerPROPlug;
  28. #else
  29. #include <A4Stuff.h>
  30. #endif
  31.  
  32.  
  33. #define tdelay        3
  34. #define tstrength    4
  35. #define enclosure    9
  36.  
  37. GDHandle    TheGDevice:0xCC8;
  38.  
  39. void AutoPosition( DialogPtr aDia)
  40. {
  41.     Point    Position, mouse;
  42.     Rect    ViewRect;
  43.     short    XSize = (aDia->portRect.right - aDia->portRect.left), YSize = (aDia->portRect.bottom - aDia->portRect.top);
  44.  
  45.  
  46.     GetMouse( &mouse);
  47.     LocalToGlobal( &mouse);
  48.  
  49.     SetRect( &ViewRect, (*TheGDevice)->gdRect.left + 8, (*TheGDevice)->gdRect.top + 43,
  50.                         (*TheGDevice)->gdRect.right - 8, (*TheGDevice)->gdRect.bottom - 8);
  51.  
  52.     Position.h = mouse.h - XSize/2;
  53.     if( Position.h + XSize >= ViewRect.right) Position.h = ViewRect.right - XSize;
  54.     else if( Position.h <= ViewRect.left) Position.h = ViewRect.left;
  55.  
  56.     Position.v = mouse.v - YSize/2;
  57.     if( Position.v + YSize >= ViewRect.bottom) Position.v = ViewRect.bottom - YSize;
  58.     else if( Position.v <= ViewRect.top) Position.v = ViewRect.top;
  59.  
  60.     MoveWindow( aDia, Position.h, Position.v, false);
  61.  
  62.     ShowWindow( aDia);
  63. }
  64.  
  65. void raiseRect (Rect theRect)
  66. {
  67.     ForeColor(whiteColor);
  68.     MoveTo(theRect.left,theRect.bottom);
  69.     LineTo(theRect.left,theRect.top);
  70.     LineTo(theRect.right,theRect.top);
  71.     ForeColor(blackColor);
  72.     LineTo(theRect.right,theRect.bottom);
  73.     LineTo(theRect.left,theRect.bottom);
  74. }
  75.  
  76. #define timeConvert        22254 //≈22KHZ
  77.  
  78. pascal void xRectProc (WindowPtr    theWindow,
  79.                         short        theItem)
  80. {
  81. short    iType;
  82. Handle    iHandle;
  83. Rect    iRect;
  84.  
  85.     GetDItem(theWindow,theItem,&iType,&iHandle,&iRect);
  86.     raiseRect( iRect);
  87. }
  88.  
  89. Boolean getParams ( short dlgID, long *p1, long *p2, PPInfoPlug *thePPInfoPlug)
  90. {
  91. DialogPtr    theDialog;
  92. Boolean        theResult = false;
  93.  
  94.     theDialog = GetNewDialog(dlgID,nil,(WindowPtr)-1);
  95.     if (theDialog) {
  96.         short    iType, itemHit;
  97.         Handle    iHandle;
  98.         Rect    iRect;
  99.         Str255    textStr;
  100.         
  101.         SetPort( theDialog);
  102.         AutoPosition( theDialog);
  103.         GetDItem(theDialog,enclosure,&iType,&iHandle,&iRect);
  104.         SetDItem(theDialog,enclosure,iType,(Handle)xRectProc,&iRect);
  105.         GetDItem(theDialog,tdelay,&iType,&iHandle,&iRect);
  106.         NumToString(*p1,textStr);
  107.         SetIText(iHandle,textStr);
  108.         GetDItem(theDialog,tstrength,&iType,&iHandle,&iRect);
  109.         NumToString(*p2,textStr);
  110.         SetIText(iHandle,textStr);
  111.         SelIText(theDialog,tdelay,0,32767);
  112.  
  113.     //    SetDialogDefaultItem(theDialog,1);
  114.  
  115.         do
  116.         {
  117.             ModalDialog( (ModalFilterProcPtr) thePPInfoPlug->MyDlgFilterUPP, &itemHit);
  118.         }
  119.         while ((itemHit != ok) && (itemHit != cancel));
  120.         
  121.         if (itemHit == ok)
  122.         {
  123.             theResult = true;
  124.             GetDItem(theDialog,tdelay,&iType,&iHandle,&iRect);
  125.             GetIText(iHandle,textStr);
  126.             StringToNum(textStr,p1);
  127.             GetDItem(theDialog,tstrength,&iType,&iHandle,&iRect);
  128.             GetIText(iHandle,textStr);
  129.             StringToNum(textStr,p2);
  130.         }
  131.         DisposDialog(theDialog);
  132.     }
  133.     return theResult;
  134. }
  135.  
  136. int checkMax (int v)
  137. {
  138.     if( v >= 127) return 127;
  139.     else if( v <= -127 ) return -127;
  140.     else return v;
  141. }
  142.  
  143. OSErr main(     Ptr                        *InstrumentPtr,
  144.                 struct FileInstrData    *theData,
  145.                 long                    SelectionStart,
  146.                 long                    SelectionEnd,
  147.                 PPInfoPlug                *thePPInfoPlug)
  148. {
  149. long    i, length,
  150.         temp1, temp2,
  151.         pDelay = 250, pStrength = 50;
  152.  
  153.     if (getParams (5000, &pDelay, &pStrength, thePPInfoPlug))
  154.     {
  155.         if (SelectionStart == SelectionEnd)
  156.         {
  157.             SelectionStart = 0;
  158.             SelectionEnd = theData->insSize;
  159.         }
  160.         length = SelectionEnd - SelectionStart - 1;
  161.  
  162.         pDelay = (pDelay * timeConvert) / 1000;    //convert ms to samples
  163.  
  164.         switch( theData->amplitude)
  165.         {
  166.             case 8:
  167.             {
  168.                 Ptr    orgPtr = (*InstrumentPtr) + SelectionStart, destPtr = orgPtr + pDelay;
  169.                 
  170.                 for( i = 0; i < (length - pDelay); i++)
  171.                 {
  172.                     temp1 = *orgPtr++;
  173.                     temp1 = (pStrength * temp1) / 100;
  174.                     
  175.                     temp2 = *destPtr;
  176.                     
  177.                     temp1 += temp2;
  178.  
  179.                     if( temp1 >= 127) temp1 = 127;    // overflow ?
  180.                     else if( temp1 <= -128 ) temp1 = -128;
  181.                     
  182.                     *destPtr++ = temp1;
  183.                 }
  184.             }
  185.             break;
  186.  
  187.             case 16:
  188.             {
  189.                 short    *orgPtr = (short*) *InstrumentPtr + (SelectionStart / 2),
  190.                 *destPtr = orgPtr + pDelay;
  191.                 
  192.                 for( i = 0; i < length / 2 - pDelay; i++)
  193.                 {
  194.                     temp1 = *orgPtr++;
  195.                     temp1 = (pStrength * temp1) / 100;
  196.                     
  197.                     temp2 = *destPtr;
  198.                     
  199.                     temp1 += temp2;
  200.                     
  201.                     if( temp1 >= (short)0x7FFF) temp1 = 0x7FFF;    // overflow ?
  202.                     else if( temp1 <= (short)0x8000 ) temp1 = (short)0x8000;
  203.                     
  204.                     *destPtr++ = temp1;
  205.                 }
  206.             }
  207.             break;
  208.         }
  209.     }
  210.     
  211.     return noErr;
  212. }